package com.microblogforsim.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.microblogforsim.R;
import com.microblogforsim.callback.JsonCallback;
import com.microblogforsim.model.LzyResponse;
import com.microblogforsim.model.MyResponse;
import com.microblogforsim.model.MyString;
import com.microblogforsim.utils.ACache;
import com.microblogforsim.utils.Urls;

public class LoginActivity extends AppCompatActivity {
    private TextView tv_login_phone;
    private Button btn_login;
    private TextView tv_forget;
    private TextView tv_help;
    private TextView tv_register;
    private ImageView img_back;
    private EditText ed_phoneOremail;
    private EditText ed_pwd;
    private ACache mCache;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        sp = getSharedPreferences("data",MODE_PRIVATE);
        editor = sp.edit();
        mCache = ACache.get(LoginActivity.this);
        initView();
    }

    private void initView() {

        tv_login_phone = findViewById(R.id.activity_login_byphone);
        btn_login = findViewById(R.id.activity_login_login);
        tv_forget = findViewById(R.id.activity_login_forget);
        tv_register = findViewById(R.id.activity_login_register);
        img_back = findViewById(R.id.activity_login_close);
        tv_help = findViewById(R.id.activity_login_help);
        btn_login.setEnabled(false);
        btn_login.setBackgroundColor(getResources().getColor(R.color.gred_c));
        ed_phoneOremail = findViewById(R.id.activity_login_account);
        ed_pwd = findViewById(R.id.activity_login_password);
        tv_login_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(LoginActivity.this, "短信验证码", Toast.LENGTH_SHORT).show();
            }
        });

        tv_forget.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(LoginActivity.this, "忘记密码", Toast.LENGTH_SHORT).show();
            }
        });
        tv_help.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(LoginActivity.this, "帮助", Toast.LENGTH_SHORT).show();
            }
        });
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LoginActivity.this.finish();
            }
        });
        tv_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, Register_Activity.class);
                LoginActivity.this.startActivity(intent);
            }
        });
    ed_phoneOremail.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {




        }

        @Override
        public void afterTextChanged(Editable s) {
            if(s.length()<1){
                btn_login.setEnabled(false);
                btn_login.setBackgroundColor(getResources().getColor(R.color.gred_c));
            }else {
                btn_login.setEnabled(true);
                btn_login.setBackgroundColor(getResources().getColor(R.color.themeColor));
            }
        }
    });
    ed_pwd.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {


        }

        @Override
        public void afterTextChanged(Editable s) {
            if(s.length()<1){
                btn_login.setEnabled(false);
                btn_login.setBackgroundColor(getResources().getColor(R.color.gred_c));
            }else {
                btn_login.setEnabled(true);
                btn_login.setBackgroundColor(getResources().getColor(R.color.themeColor));
            }
        }
    });
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String phone = "";
                String email = "";
                String pwd = "";
                phone = ed_phoneOremail.getText().toString();

                if(!phone.equals("")&&!ed_pwd.getText().toString().equals("")){
                    if (phone.contains("@")) {
                        email = ed_phoneOremail.getText().toString();
                        phone = null;
                    } else {
                        email = null;
                        phone = ed_phoneOremail.getText().toString();
                    }
                    OkGo.<MyResponse>post(Urls.SERVER + "User/login")//
                            .tag(this)//
                            .headers("header1", "headerValue1")//
                            .params("phone", phone)//
                            .params("pwd", ed_pwd.getText().toString())//
                            .params("email", email)//

                            .isMultipart(true)         //强制使用 multipart/form-data 表单上传（只是演示，不需要的话不要设置。默认就是false）
                            .execute(new JsonCallback<MyResponse>() {
                                @Override
                                public void onSuccess(Response<MyResponse> response) {
                                    Log.d("TAG", "onSuccess: " + response.body());
                                    if (response.body().msg .equals("success") ) {
                                        Toast.makeText(LoginActivity.this, "登录成功"+response.body().data, Toast.LENGTH_SHORT).show();
                                        SharedPreferences sharedPreferences= getSharedPreferences("data", Context.MODE_PRIVATE);
                                        SharedPreferences.Editor editor = sharedPreferences.edit();
                                        editor.putString("openid",response.body().data+"");
                                        editor.commit();
                                        mCache.put("openid", response.body().data);
                                        editor.putString("openid", response.body().data+"");
                                        editor.commit();
                                        Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                                        intent.putExtra("page",4);
                                        intent.putExtra("openid",response.body().data+"");
                                        LoginActivity.this.startActivity(intent);
                                        finish();

                                    }else {
                                        Toast.makeText(LoginActivity.this, "账号或密码不正确", Toast.LENGTH_SHORT).show();
                                    }
                                }

                                @Override
                                public void onError(Response<MyResponse> response) {
                                    Toast.makeText(LoginActivity.this, "查询登陆用户出错", Toast.LENGTH_SHORT).show();
                                    Log.d("TAG", "err: " + response.body());
                                }
                            });
                }else{
                    Toast.makeText(LoginActivity.this, "账号或密码为空", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }
}